﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using CardLogistics.Model.Basic;
using System.Data;

namespace CardLogistics.DAL.Basic
{
    /// <summary>
    /// 基础数据（0 芯片类型、1 采购类型）
    /// </summary>
    public class Basic_BasicTypeData
    {
        #region 新增
        /// <summary>
        /// 新增销售机构/网点信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Add(Basic_BasicType model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,100),
					new OracleParameter("vTYPENAME", OracleType.VarChar),
					new OracleParameter("vTYPENO", OracleType.Number),
					new OracleParameter("vREMARK", OracleType.VarChar),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar),
					new OracleParameter("vSTATUS", OracleType.Number),
					new OracleParameter("vISDELETE", OracleType.Number),
                    new OracleParameter("vCHIPMODEL",OracleType.VarChar)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.TYPENAME;
                parameters[2].Value = model.TYPENO;
                parameters[3].Value = model.REMARK;
                parameters[4].Value = model.LASTUPDATEUSER;
                parameters[5].Value = model.STATUS;
                parameters[6].Value = model.ISDELETE;
                parameters[7].Value = model.CHIPMODEL;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Basic_BasicType_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>修改成功返回true、否则返回false</retu     rns>
        public string Update(Basic_BasicType model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar),
					new OracleParameter("vTYPENAME", OracleType.VarChar),
					new OracleParameter("vTYPENO", OracleType.Number),
					new OracleParameter("vREMARK", OracleType.VarChar),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar),
					new OracleParameter("vSTATUS", OracleType.Number),
					new OracleParameter("vISDELETE", OracleType.Number),
                     new OracleParameter("vCHIPMODEL",OracleType.VarChar),
                    new OracleParameter("varOut", OracleType.VarChar,100)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.TYPENAME;
                parameters[2].Value = model.TYPENO;
                parameters[3].Value = model.REMARK;
                parameters[4].Value = model.LASTUPDATEUSER;
                parameters[5].Value = model.STATUS;
                parameters[6].Value = model.ISDELETE;
                parameters[7].Value = model.CHIPMODEL;
                parameters[8].Direction = ParameterDirection.Output;
                return DbHelperSQL.RunProcedure("P_Basic_BasicType_Update", parameters, parameters[8]).ToString();
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("vID",  OracleType.VarChar)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Basic_BasicType_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "Basic_BasicType", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }
        #endregion

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Basic_BasicType GetModel(string Id)
        {
            Basic_BasicType model = new Basic_BasicType();
            DataRow row = GetInfoById(Id).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["TYPENAME"] != null)
                {
                    model.TYPENAME = row["TYPENAME"].ToString();
                }
                if (row["TYPENO"] != null && row["TYPENO"].ToString() != "")
                {
                    model.TYPENO = decimal.Parse(row["TYPENO"].ToString());
                }
                if (row["REMARK"] != null)
                {
                    model.REMARK = row["REMARK"].ToString();
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["STATUS"] != null && row["STATUS"].ToString() != "")
                {
                    model.STATUS = decimal.Parse(row["STATUS"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = decimal.Parse(row["ISDELETE"].ToString());
                }
                if (row["CHIPMODEL"] != null)
                {
                    model.CHIPMODEL = row["CHIPMODEL"].ToString();
                }
            }
            return model;
        }
    }
}